using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace MultiVMR9cs
{
    public class MultiVMR9Panel : Panel
    {
        private MultiVMR9Renderer _renderer;
        private VMR9DisplayMode _displayMode;

        public MultiVMR9Panel(VMR9DisplayMode mode)
        {
            _displayMode = mode;
            _renderer = new MultiVMR9Renderer(this, mode);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.UpdateStyles();
        }

        public PresentParameters PresentParameters
        {
            get
            {
                return _renderer.PresentParameters;
            }
            set
            {
                _renderer.PresentParameters = value;
            }
        }

        public VMR9DisplayMode DisplayMode
        {
            get
            {
                return _displayMode;
            }
        }

        public FullScreenMode FullScreen
        {
            get
            {
                return _renderer.FullScreenMode;
            }
            set
            {
                _renderer.FullScreenMode = value;
            }
        }

        public MultiVMR9Renderer Renderer
        {
            get
            {
                return _renderer;
            }
        }

        
        public static int Adapters
        {
            get
            {
                return Manager.Adapters.Count;
            }
        }

        public static List<VMR9DisplayMode> GetDisplayModes(int adapter)
        {
            List<VMR9DisplayMode> modes = new List<VMR9DisplayMode>();

            if (adapter > -1 && adapter < Manager.Adapters.Count)
            {
                foreach (DisplayMode dm in Manager.Adapters[adapter].SupportedDisplayModes)
                {
                    modes.Add(new VMR9DisplayMode(adapter,dm,FullScreenMode.None));
                }
            }

            return modes;
        }

        public void ResetDevice(VMR9DisplayMode mode)
        {
            _renderer.ResetDevice(mode);
        }

        public void ResetDevice()
        {
            _renderer.ResetDevice(false);
        }
    }

    /// <summary>
    /// We'll use a simpe class to keep screen formats so the end user only has to include references to
    /// MultiVMR9cs and not Managed DirectX (for simplicity's sake)
    /// </summary>
    public class VMR9DisplayMode
    {
        private DisplayMode _dm;
        protected int _adapter;
        FullScreenMode _fullScreenMode;

        internal VMR9DisplayMode(int adapter, DisplayMode dm, FullScreenMode fullScreenMode)
        {
            _adapter = adapter;
            _dm = dm;
            _fullScreenMode = fullScreenMode;
        }

        public FullScreenMode FullScreenMode
        {
            get
            {
                return _fullScreenMode;
            }
            set
            {
                _fullScreenMode = value;
            }
        }

        public int Adapter
        {
            get
            {
                return _adapter;
            }
        }
        
        public int Width
        {
            get
            {
                return _dm.Width;
            }
        }

        public int Height
        {
            get
            {
                return _dm.Height;
            }
        }

        internal Format Format
        {
            get
            {
                return _dm.Format;
            }
        }

        public string FormatString
        {
            get
            {
                return _dm.Format.ToString();
            }
        }

        public int RefreshRate
        {
            get
            {
                return _dm.RefreshRate;
            }
        }

        public override string ToString()
        {
            return Width.ToString() + " x " + Height.ToString() + " : " + Format + " " + RefreshRate.ToString() + " htz";
        }
    }
}
